﻿@page "/forum"
@using AntDesign
@inject NavigationManager Nav

<PageHeader 
Title="文章列表" 
Subtitle="最新技术分享"/>

<!-- 搜索与统计区域 -->
<div class="search-stats">
    <Input @bind-Value=@searchKeyword
    Placeholder="输入文章标题/作者/分类..." 
    OnKeyDown="HandleKeydown"
    Style="width: 300px;"
    />
    <div class="stats">
        <Tag Color="@("red")">全部 <Badge Count="@allArticles.Count" /></Tag>
        <Tag Color="@("blue")">技术类 <Badge Count="@techCount" /></Tag>
        <Tag Color="@("orange")">教程类 <Badge Count="@tutorialCount" /></Tag>
    </div>
</div>

<!-- 文章卡片列表 -->
<Row Gutter="24" Style="margin: 24px 0">
    <!-- 第一行 -->
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[0]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[1]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[2]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[3]" />
    </Col>

    <!-- 第二行 -->
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[4]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[5]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[6]" />
    </Col>
    <Col Span="6">
        <ArticleCard Article="@DisplayArticles[7]" />
    </Col>
</Row>

<!-- 分页控件 -->
<Pagination Total="@totalItems"
PageSize="8"
Current="@currentPage"
OnChange="HandlePageChange"
Style="text-align: center; margin: 40px 0"
ShowSizeChanger="false" />

@code {
    private List<Article> allArticles = new();
    private List<Article> filteredArticles = new();
    private int totalItems = 0;
    private int currentPage = 1;
    private string searchKeyword = "";
    private int techCount => allArticles.Count(a => a.Category == "技术");
    private int tutorialCount => allArticles.Count(a => a.Category == "教程");

    protected override async Task OnInitializedAsync()
    {
        await LoadMockData();
        ApplyFilters();
    }
    private void ApplyFilters()
    {
        filteredArticles = allArticles
            .Where(a =>
                a.Title.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase) ||
                a.Category.Contains(searchKeyword, StringComparison.OrdinalIgnoreCase))
            .ToList();
    }


    private List<Article> DisplayArticles =>
        filteredArticles
            .Skip((currentPage - 1) * 8)
            .Take(8)
            .Concat(Enumerable.Repeat(new Article(), 8)) // 填充空对象
            .Take(8) // 确保返回8条
            .ToList();

    private void HandleSearch(string keyword)
    {
        searchKeyword = keyword;
        currentPage = 1;
        ApplyFilters();
    }

    private void HandleKeydown(KeyboardEventArgs args)
    {
        if (args.Key == "Enter")
        {
            HandleSearch(searchKeyword);
        }
    }

    private async Task LoadMockData()
    {
        // 生成模拟数据
        var mockData = new List<Article>();
        List<Article> showcase = new List<Article>
        {
            new Article()
            {
                Id = 1,
                Title = $"C语言指针的巧妙利用（1）",
                 CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                 Category =  "教程",
                  AuthorId = 1001
            },
            new Article()
            {
                Id = 2,
                Title = $"GNU GCC安装教程 Windows篇",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "教程",
                AuthorId = 1001
            },
            new Article()
            {
                Id = 3,
                Title = $"C Programming Philosophy",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "技术",
                AuthorId = 1002
            },
            new Article()
            {
                Id = 4,
                Title = $"经典内存架构分析--C语言视角",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "技术",
                AuthorId = 1002
            },
            new Article()
            {
                Id = 5,
                Title = $"C语言指针的巧妙利用（2）",
                 CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                 Category =  "教程",
                  AuthorId = 1001
            },
            new Article()
            {
                Id = 6,
                Title = $"GNU GCC安装教程 Debian篇",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "教程",
                AuthorId = 1001
            },
            new Article()
            {
                Id = 7,
                Title = $"Csharp Programming Philosophy",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "技术",
                AuthorId = 1002
            },
            new Article()
            {
                Id = 8,
                Title = $"操作系统进程调度--C语言视角",
                CreateTime = (int)DateTimeOffset.Now.AddHours(1).ToUnixTimeSeconds(),
                Category =  "技术",
                AuthorId = 1002
            },

        };

        mockData.AddRange(showcase);
        mockData.AddRange(Enumerable.Range(8, 28).Select(i => new Article
            {
                Id = i,
                Title = $"技术文章 {i}",
                Content = $"这是第{i}篇文章的摘要内容...",
                CreateTime = (int)DateTimeOffset.Now.AddHours(-i).ToUnixTimeSeconds(),
                Category = i % 3 == 0 ? "教程" : "技术",
                AuthorId = i % 2 == 0 ? 1001 : 1002
            }).ToList());
        allArticles = mockData;
        filteredArticles = allArticles;
        totalItems = mockData.Count;
    }

    private void HandlePageChange(PaginationEventArgs args)
    {
        currentPage = args.Page;
    }
}
